package com.sp2.dev.controller;


import com.sp2.dev.bean.User;
import com.sp2.dev.constant.MessageProperties;
import com.sp2.dev.jdbc.exception.DBConnection;
import com.sp2.dev.util.PicRgbUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by IntelliJ IDEA.
 * User: zhimingli
 * Date: 14-1-22
 * Time: 上午9:13
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class TestController {


    private static final Logger logger = LoggerFactory.getLogger(TestController.class);


    @RequestMapping(value = "/test/color")
    public ModelAndView color(HttpServletRequest request) {
        String path = request.getParameter("color");
        String color = "";
        if (!StringUtils.isEmpty(path)) {
            color = PicRgbUtil.getImagePixel(path);
        }
        request.setAttribute("color", color);
        return new ModelAndView("/jsp/test/color");
    }

    @RequestMapping(value = "/test/test")
    public ModelAndView test(HttpServletRequest request, @RequestParam(value = "val", required = false) String val) throws UnsupportedEncodingException {
        logger.debug("---------------debug-----------------------");
        logger.info("---------------info-----------------------");
        logger.error("---------------error-----------------------");
        System.out.println("test method");

        String test_message = MessageProperties.test_message;
        System.out.println(test_message);

        String first = request.getParameter("joyme");
        if (StringUtils.isEmpty(first)) {
            first = request.getSession().getAttribute("joyme") + "";

        }
        System.out.println(first);
        request.setAttribute("first", first);
        request.getSession().removeAttribute(first);


        String aaa = request.getParameter("aaa");
        System.out.println("aaa ====" + aaa);

        System.out.println(val);
        return new ModelAndView("/jsp/test/test");
    }


    @RequestMapping(value = "/test/testval")
    public String testval(HttpServletRequest request) throws UnsupportedEncodingException {
        String first = "4564654787987";
        request.getSession().setAttribute("joyme", "着迷网");
        return "redirect:/test/test?first=" + first;
    }

    @RequestMapping(value = "/test/forword")
    public ModelAndView forword(HttpServletRequest request, RedirectAttributes redirectAttributes) throws UnsupportedEncodingException {
        String first = "显示中文显示中文";
        Map<String, String> map = new HashMap<String, String>();
        map.put("val", first);
        System.out.println("/test/forword");
        redirectAttributes.addAttribute("aaa", "bbb");
        // return "forward:/test/test";
        return new ModelAndView("redirect:/test/test");
        //return  new ModelAndView("redirect:test",map);
    }


    @RequestMapping(value = "/test/conn")
    public ModelAndView con(HttpServletRequest request) {
        DBConnection db = new DBConnection();
        Connection coon = db.getConnection();
        request.setAttribute("conn", coon);
        return new ModelAndView("/jsp/test/conn");
    }

    //展示list数据
    @RequestMapping(value = "/test/list")
    public ModelAndView list(HttpServletRequest request) {
        List<User> list = new ArrayList<User>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setName("name" + i);
            user.setAge(1 + i);
            list.add(user);
        }
        request.setAttribute("list", list);
        return new ModelAndView("/jsp/test/list");
    }

    @RequestMapping(value = "method", method = RequestMethod.GET)
    public ModelAndView get(HttpServletRequest request) {
        request.setAttribute("method", "get");
        return new ModelAndView("/jsp/test/method");
    }

    @RequestMapping(value = "method", method = RequestMethod.POST)
    public ModelAndView post(HttpServletRequest request) {
        request.setAttribute("method", "post");
        return new ModelAndView("/jsp/test/method");
    }

    ///////////////////-----------------------------
    @RequestMapping(value = "/test/getPojoView")
    public String getPojoView(Model model) {
        User user = new User();
        user.setName("getpojoview");
        user.setAge(1111);
        model.addAttribute(user);
        return "/jsp/test/";
    }

    @RequestMapping(value = "/test/getPojoView2")
    public String getPojoView2(Model model) {
        User user = new User();
        user.setName("getpojoview");
        user.setAge(1111);
        model.addAttribute(user);
        return "sample/pojoView";
    }


    public static void main(String[] args) throws UnsupportedEncodingException {
        Long now = System.currentTimeMillis();
        Long after = 1390978492942L + 604800000L;
        System.out.println(now);
        System.out.println(after);
        System.out.println(now - after);

        String first2 = new String("中文中文".getBytes(), "utf-8");
        System.out.println(first2);
    }


}
